﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITD_VAPCF.NganQuy.BLL;
using ITD_VAPCF.NganQuy.ENTITY;
using ITD_VAPCF.NganQuy.REPORT;

namespace ITD_VAPCF.NganQuy.GUI
{
    public partial class CountMoney : UserControl
    {
        private DataTable _dtTable = new DataTable();
        private string _soChungTu = "";
        private string _giaoDichID = "";
        private string _loaiGiaoDichID = "";
        private KKTienMatBLL objNQ_GiaoDich;
        private KKTienMat _entityTienMat = new KKTienMat();
        private TTKhachHang _entityKhachHang = new TTKhachHang();
        private CrystalDecisions.CrystalReports.Engine.ReportClass report;
        public DataTable DtTable
        {
            get { return _dtTable; }
            set { _dtTable = value; }
        }
        public string LoaiGiaoDichID
        {
            get { return _loaiGiaoDichID; }
            set { _loaiGiaoDichID = value; }
        }
        public string SoChungTu
        {
            get { return _soChungTu; }
            set { _soChungTu = value; }
        }
        public string GiaoDichID
        {
            get { return _giaoDichID; }
            set
            {
                _giaoDichID = value;
                objNQ_GiaoDich = new KKTienMatBLL(CommonVAPCF.GlobalVariables.g_strConnectionMsSql);
                DataTable dt = objNQ_GiaoDich.SelectOnlyOneEntityByID(value);
                LoadNhapBangKe();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {

                    }
                }
            }
        }
        public KKTienMat EntityTienMat
        {
            get { return _entityTienMat; }
            set { _entityTienMat = value; }
        }
        public DateTime NgayGiaoDich
        {
            set { txt_ngayGD.Value = value; }
            get { return txt_ngayGD.Value; }
        }
        public TTKhachHang EntityKhachHang
        {
            get { return _entityKhachHang; }
            set { _entityKhachHang = value; }
        }
        public void LoadThongTinGiaoDich(string _sgiaoDichID, string _sloaiGiaoDichID, string _ssoChungTu, double _soTien)
        {
            GiaoDichID = _sgiaoDichID;
            LoaiGiaoDichID = _sloaiGiaoDichID;
            SoChungTu = _ssoChungTu;
            SoTienGiaoDich = _soTien;
        }
        
        public void LoadNhapBangKe()
        {
            DtTable = NgayQuyBLL.Get_LoaiTien();
            dtgvBangKe.AutoGenerateColumns = false;
            dtgvBangKe.DataSource = _dtTable.DefaultView;

            ConfigDataGrid();
        }
        private void ConfigDataGrid()
        {
            
        }
        private double _sOTienGiaoDich;
        public double SoTienGiaoDich
        {
            get { return _sOTienGiaoDich; }
            set
            {
                _sOTienGiaoDich = value;
                txt_So_tien.Double = value;
            }
        }
        public CountMoney()
        {
            InitializeComponent();
            f_loaitien.SelectedIndex = 0;
        }
        private void DoFilter()
        {
            switch (f_loaitien.SelectedIndex)
            {
                case 0:
                    _dtTable.DefaultView.RowFilter = "";
                    break;
                case 1:
                    _dtTable.DefaultView.RowFilter = "substring(loai_tien_id,1,1) = 1";
                    break;
                case 2:
                    _dtTable.DefaultView.RowFilter = "substring(loai_tien_id,1,1) = 2";
                    break;
                case 3:
                    _dtTable.DefaultView.RowFilter = "substring(loai_tien_id,1,1) = 3";
                    break;
                default:
                    break;
            }
        }
        public void Save()
        {
            _entityTienMat = new KKTienMat();
            _entityTienMat.GiaoDichID = GiaoDichID;
            objNQ_GiaoDich.Delete(GiaoDichID);
            for (int i = 0; i < dtgvBangKe.Rows.Count; i++)
            {
                double dThu = 0, dChi = 0;
                if (dtgvBangKe["Thu", i].Value != null) dThu = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", i].Value);
                if (dtgvBangKe["Chi", i].Value != null) dChi = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", i].Value);
                if (dThu + dChi > 0)
                {
                    _entityTienMat.Loai_Giao_Dich = _loaiGiaoDichID;
                    _entityTienMat.LoaiTienID = dtgvBangKe["loai_tien_id", i].Value.ToString();
                    _entityTienMat.SoChungTu = SoChungTu;
                    _entityTienMat.Ngay = txt_ngayGD.Value.Date;
                    _entityTienMat.SoTien = txt_So_tien.Double;
                    _entityTienMat.Thu = dThu;
                    _entityTienMat.Chi = dChi;
                    _entityTienMat.TienTe = "00";
                    _entityTienMat.TonCuoiKy = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonCuoiKy", i].Value);
                    _entityTienMat.TonDauKy = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonDauKy", i].Value);
                    _entityTienMat.UsersNew = "";
                    _entityTienMat.UsersModify = "";
                    objNQ_GiaoDich.Insert(_entityTienMat);

                }
            }
            CommonVAPCF.MessagesReturn.ShowDialog("Đã xong");
        }

        private void dtgvBangKe_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dtgvBangKe.EndEdit();

                if (dtgvBangKe[0, e.RowIndex].Value.ToString().Length < 3)
                {

                    dtgvBangKe["Thu", e.RowIndex].Value = null;
                    dtgvBangKe["Chi", e.RowIndex].Value = null;
                    return;
                }
                string colname = dtgvBangKe.Columns[e.ColumnIndex].Name;
                double dThu = 0;
                double dChi = 0;
                double dTonDK = 0;
                dTonDK = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonDauKy", e.RowIndex].Value);
                dThu = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", e.RowIndex].Value);
                dChi = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", e.RowIndex].Value);
                dtgvBangKe.Rows[e.RowIndex].Cells["TonCuoiKy"].Value = dTonDK + dThu - dChi;
                string MenhGia = CommonVAPCF.FuncUtilities.DecodeMoney(dtgvBangKe["loai_tien", e.RowIndex].Value.ToString(), ",");
                double Sum = CommonVAPCF.ConvertData.ToDouble(MenhGia) * (dTonDK + dThu - dChi);
                dtgvBangKe.Rows[e.RowIndex].Cells["SoTien"].Value = Sum;
            }
            catch (Exception ex)
            {
                CommonVAPCF.LogFile.WriteLog(ex, this.Name, "dtgvBangKe_CellEndEdit");
                CommonVAPCF.MessagesReturn.ShowDialog("Lỗi : " + ex.ToString());
            }
        }

        private void f_loaitien_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_dtTable != null)
                DoFilter();
        }
        private void dtgvSoTietKiem_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txtbox = e.Control as TextBox;
            if (txtbox != null)
            {
                txtbox.KeyPress += new KeyPressEventHandler(txtbox_KeyPress);
                txtbox.TextChanged += new EventHandler(txtbox_TextChanged);
            }
        }
        private void txtbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && !(e.KeyChar == '.'))
                e.Handled = true;
        }
        private void txtbox_TextChanged(object sender, EventArgs e)
        {
            TextBox txtbox = sender as TextBox;
            string TextTmp = txtbox.Text.Trim();
            txtbox.Text = CommonVAPCF.FuncUtilities.EncodeMoney(CommonVAPCF.FuncUtilities.DecodeMoney(TextTmp, ","), ",");
            txtbox.SelectionStart = 20;
        }
        public void InChungTu()
        {
            
            DataTable tmp = DtTable.DefaultView.ToTable(true,"loai_tien_id","loai_tien", "Thu", "Chi");
            DataRow[] rows = DtTable.Select("len(loai_tien_id)<3");
            tmp.Columns[0].ColumnName = "LoaiTien";
            tmp.Columns[1].ColumnName = "MenhGia";
            tmp.Columns[2].ColumnName = "A2";
            tmp.Columns[3].ColumnName = "A3";
            for (int i = 0; i < tmp.Rows.Count; i++)
            {
                tmp.Rows[i]["LoaiTien"] = tmp.Rows[i]["LoaiTien"].ToString().Substring(0, 1);
                string Mg =  CommonVAPCF.FuncUtilities.DecodeMoney(tmp.Rows[i]["MenhGia"].ToString(),",");
                tmp.Rows[i]["A3"] = CommonVAPCF.ConvertData.ToDouble(tmp.Rows[i]["A2"].ToString()) * CommonVAPCF.ConvertData.ToDouble(Mg);
            }
            for (int i = 0; i < tmp.Rows.Count; i++)
            {
                for (int j = 0; j < rows.Length; j++)
                {
                    if (tmp.Rows[i]["LoaiTien"].ToString() == rows[j][0].ToString())
                        tmp.Rows[i]["loaitien"] = rows[j][1].ToString();
                }
            }
            report = new BangKeLoaiTienThu();
            tmp.DefaultView.RowFilter = "A3>0";
            report.SetDataSource(tmp.DefaultView);
            report.SetParameterValue("Ten_Don_vi", Sys_Infor_Company.g_sCompanyName.ToUpper());
            report.SetParameterValue("Dia_chi_Don_vi", Sys_Infor_Company.g_sCompanyAddress);
            report.SetParameterValue("User_id", Common.GlobalUser.gs_UserName);
            report.SetParameterValue("Mau_so", "11/KT");
            report.SetParameterValue("Ten_BC", "BẢNG KÊ LOẠI TIỀN THU");
            report.SetParameterValue("Ten_Khach_hang", EntityKhachHang.TenKhachHang);
            report.SetParameterValue("Dia_chi", EntityKhachHang.DiaChi);
            report.SetParameterValue("Di_dong", EntityKhachHang.SoDienThoai);
            report.SetParameterValue("Noi_dung", EntityKhachHang.NoiDung);
            report.SetParameterValue("Tien_Bang_Chu", Common.Functions.MoneyToString(txt_So_tien.Double.ToString(), true));
            report.SetParameterValue("Ngay_thang_nam", txt_ngayGD.Text);
            report.SetParameterValue("Nguoi_nop_tien", "Người nộp tiền");
            report.SetParameterValue("Nguoi_thu_tien", "Người thu tiền");
            report.SetParameterValue("Ten_khach_hang", EntityKhachHang.TenKhachHang);
            report.SetParameterValue("Giao_dich_vien", Common.GlobalUser.gs_FullName);

            CommonVAPCF.frmPrintPreview frm = new CommonVAPCF.frmPrintPreview();
            frm.LoadDataPrint(report);
            frm.ShowDialog();
        }
    }
}
